#include "stdafx.h"

//*****************************************************************************
int initPlateMaterialProperty(material *substance) 
{
	int k;
	//Plate
	k = 0;
	substance[k].id = 0; 
	sprintf(substance[k].name, "Plate - 22MnB5 Steel");
	substance[k].density = 7830;			//UNIT:kg/m^3
	substance[k].htc = 32;					//UNIT:W/(m*K)
	substance[k].sh = 650;					//UNIT:J/(kg*K)
	substance[k].initTemp = PLATE_INIT_TEMP;//UNIT:K

	//Die Up
	k = 1;
	substance[k].id = 1;
	sprintf(substance[k].name, "Die Up - 5CrMnMo Steel");
	substance[k].density = 7500;
	substance[k].htc = 35.23;
	substance[k].sh = 678.5;
	substance[k].initTemp = AIR_INIT_TEMP;

	//Die Down
	k = 2;
	substance[k].id = 2;
	sprintf(substance[k].name, "Die Down - HT200 - w(C) = 3%");
	substance[k].density = 7570;
	substance[k].htc = 39.2;
	substance[k].sh = 470;
	substance[k].initTemp = AIR_INIT_TEMP;

	//Gas
	k = 3;
	substance[k].id = 3;
	sprintf(substance[k].name, "Gas - Air");
	substance[k].density = 1.205;
	substance[k].htc = 0.0259;
	substance[k].sh = 1005;
	substance[k].initTemp = AIR_INIT_TEMP;

	substance[0].both[0] = 0; substance[1].both[0] = 0; substance[2].both[0] = 0; substance[3].both[0] = 20000;
	substance[0].both[1] = 0; substance[1].both[1] = 0; substance[2].both[1] = 0; substance[3].both[1] = 20000;
	substance[0].both[2] = 0; substance[1].both[2] = 0; substance[2].both[2] = 0; substance[3].both[2] = 20000;
	substance[0].both[3] = 20000; substance[1].both[3] = 20000; substance[2].both[3] = 20000; substance[3].both[3] = 0;

	return 1;
}

//*****************************************************************************
int initPlateHtcInter(material *substance, double dx, 
					  double htcInter[NUM_MATERIAL][NUM_MATERIAL]) 
{
	double r, k;
	for(int i = 0; i < NUM_MATERIAL; i++)
		for(int j = i; j < NUM_MATERIAL; j++)
		{
			if (substance[i].both[j] == 0) 
				r = 0; 
			else 
				r = 1 / substance[i].both[j];
			if (i == 0 && (j == 1 || j == 2)) 
				k = TOUCH_FACTOR; 
			else 
				k = 1;
			htcInter[i][j] = htcInter[j][i] = k * dx / (r + dx / 2 / substance[i].htc + dx / 2 / substance[j].htc);
			if (i == 0 && (j == 1 || j == 2)) 
				htcInter[i][j] = htcInter[j][i] = 7;
		}

	return 1;
}

//*****************************************************************************
int initPlateBlock(int up, int down, int left, int right, int id, int **grid)
{
	for(int i = up; i < down; i++)
		for(int j = left; j < right; j++)
			grid[i][j] = id;

	return 1;
}

//*****************************************************************************
//init plate 2D
int** plate2DInit(material *substance, double htcInter[NUM_MATERIAL][NUM_MATERIAL], 
				  double *dx, int *row, int *col,
				  double *tempEdge, terminationCondition2D *tc)
{
	//substance
	initPlateMaterialProperty(substance);
	//dx
	*dx = 0.0001;
	//htcInter
	initPlateHtcInter(substance, *dx, htcInter);
	//row + col
	double	plateHeight				= 0.002;
	double	plateWidth				= 0.05;
	double	plateDieUpHeight		= 0.02;
	double	plateDieUpWidth			= 0.08;
	double	plateDieDownHeight		= 0.03;
	double	plateDieDownWidtth		= 0.08;
	double	airThick				= 0.002;

	int		plateHeightNum			= plateHeight / *dx;
	int		plateWidthNum			= plateWidth / *dx;
	int		plateDieUpHeightNum		= plateDieUpHeight / *dx;
	int		plateDieUpWidthNum		= plateDieUpWidth / *dx;
	int		plateDieDownHeightNum	= plateDieDownHeight / *dx;
	int		plateDieDownWidtthNum	= plateDieDownWidtth / *dx;
	int		airThickNum				= airThick / *dx;

	*row = airThickNum + plateDieUpHeightNum + plateHeightNum + plateDieDownHeightNum +  airThickNum;
	*col = airThickNum + (plateDieUpWidthNum > plateDieDownWidtthNum ? plateDieUpWidthNum : plateDieDownWidtthNum) + airThickNum;
	//grid
	int **grid = (int **)malloc(sizeof(int *) * (*row));
	for(int i = 0; i < *row; i++) grid[i] = (int *)malloc(sizeof(int) * (*col));

	int up, down, left, right, k;
	//init block - Gas - Air
	k = 3;
	up = 0; down = *row; left = 0; right = *col; 
	initPlateBlock(up, down, left, right, substance[k].id, grid);
	//init block - Plate - 
	k = 0;
	up = airThickNum + plateDieUpHeightNum; down = up + plateHeightNum; 
	left = (*col - plateWidthNum) / 2; right = left + plateWidthNum; 
	initPlateBlock(up, down, left, right, substance[k].id, grid);
	//init block - Die Up -  
	k = 1;
	up = airThickNum; down = up + plateDieUpHeightNum; 
	left = (*col - plateDieUpWidthNum) / 2; right = left + plateDieUpWidthNum;
	initPlateBlock(up, down, left, right, substance[k].id, grid);
	//init block - Die Down - 
	k = 2;
	up = airThickNum + plateDieUpHeightNum + plateHeightNum; down = up + plateDieDownHeightNum; 
	left = (*col -  plateDieDownWidtthNum) / 2; right = left + plateDieDownWidtthNum; 
	initPlateBlock(up, down, left, right, substance[k].id, grid);	

	////edge
	//for(int i = 0; i < *row; i++)
	//	grid[i][0] = grid[i][*col - 1] = EDGE_ID;
	//for(int i = 0; i < *col; i++)
	//	grid[0][i] = grid[*row - 1][i] = EDGE_ID;

	//tempEdge
	*tempEdge = substance[3].initTemp;

	//tc
	(*tc).x = *col / 2;
	(*tc).y = airThickNum + plateDieUpHeightNum + plateHeightNum / 2;
	(*tc).terminationTemp = TERMINATION_TEMPERATION;

	return grid;
}